import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import store from './store'

import router from "./router"
import ElementPlus from "element-plus"
import 'element-plus/dist/index.css'
import './assets/less/index.less'
import './api/mock.js'
import api from "./api/api.js"
const app= createApp(App)
 app.use(ElementPlus)
 import * as ElementPlusIconsVue from '@element-plus/icons-vue'

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
app.config.globalProperties.$api=api
console.log(store,'store')
store.commit('setMenu',[store.state.menu,router])
function checkRouter(path){
  let hasCheck=router.getRoutes().filter(router=>router.path==path).length
  return hasCheck?true:false
}
router.beforeEach((to,from,next)=>{
  store.commit('getToken')
 
  const token=store.state.token
 
  if(!token && to.path!=='/login'){
  console.log('路由守卫1',token)

    next({path:'/login'})
  }else if(!checkRouter(to.path||to.name)){
    next({name:'home'})
  }
  else{
  console.log('路由守卫2',token)

    next()
  }
})
 app.use(store).use(router)
 app.mount('#app')
